package com.elsa.smart_buffet.service.impl;

import com.elsa.smart_buffet.mapper.MenuPriceMapper;
import com.elsa.smart_buffet.pojo.ResultBox.CallBackMessage;
import com.elsa.smart_buffet.pojo.ResultBox.ResponseResult;
import com.elsa.smart_buffet.pojo.po.CategoryPrice;
import com.elsa.smart_buffet.pojo.po.MenuPrice;
import com.elsa.smart_buffet.pojo.vo.CategoryPriceStats;
import com.elsa.smart_buffet.pojo.vo.MenuPriceStats;
import com.elsa.smart_buffet.service.MenuPriceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : MenuPriceServiceImpl  //类名
 * @Author : elsa //作者
 */
@Service
@Slf4j
@Transactional
public class MenuPriceServiceImpl implements MenuPriceService {

    @Autowired
    private MenuPriceMapper menuPriceMapper;

    @Override
    public ResponseResult getMenuPriceByTime(String data) {
        log.info(String.valueOf(menuPriceMapper.getMenuPriceByTime(data)));
        List<MenuPrice> list = menuPriceMapper.getMenuPriceByTime(data);
        List<MenuPriceStats> menuPriceStatsList = new ArrayList<>();
        int totalPrice = 0;
        int totalNum = 0;
        for (int i = 0; i < list.size(); i++) {
            MenuPriceStats menuPriceStats = new MenuPriceStats();
            menuPriceStats.setName(list.get(i).getName());
            totalNum += list.get(i).getNum();
            menuPriceStats.setNum(list.get(i).getNum());
            totalPrice += list.get(i).getPrice();
            menuPriceStats.setTotalPrice(list.get(i).getPrice());
            menuPriceStatsList.add(menuPriceStats);
        }
        for (int i = 0; i < list.size(); i++) {
            menuPriceStatsList.get(i).setRatio(String.valueOf(Math.round((double) menuPriceStatsList.get(i).getNum() / totalNum * 10000) / 100) + '%');
            menuPriceStatsList.get(i).setTotalPriceRatio(String.valueOf(Math.round((double) menuPriceStatsList.get(i).getTotalPrice() / totalPrice * 10000) / 100) + '%');
        }
        log.info(String.valueOf(menuPriceStatsList));
        return new ResponseResult(CallBackMessage.SUCCESS, menuPriceStatsList);
    }
}
